using System;
using System.Collections;
using System.Web.Caching;
using CommunityServer.Components;

namespace FourRoads.Links.Components
{
	internal class ResLinkRssWriter : BaseRssWriter
	{
		public ResLinkRssWriter(ArrayList posts, ResLinkSection s, string baseUrl):base(posts,s, baseUrl)
		{
				
		}
		
		protected override string EnclosureUrl(PostAttachmentMetaData pa)
		{
			return string.Empty;
		}

		protected ResLinkSection CurrentLinkSection
		{
			get{ return CurrentSection as ResLinkSection;}
		}

		protected override string BuildLink(Post p)
		{
			return ResLinkUrls.Instance().ViewLink(CurrentLinkSection.SectionID ,  p.PostID);
		}

		protected override bool AllowComments
		{
			get
			{
				return false;
				//return base.AllowComments && CurrentLinkSection.EnableComments;
			}
		}

		protected override void PostComments(Post p)
		{
			base.PostComments (p);
		}


		protected override void WriteChannel()
		{
			BuildChannel(ResLinkUrls.Instance().ViewSection(CurrentLinkSection.SectionID),CurrentSection.Description);
		}
	}
	/// <summary>
	/// Summary description for ResLinkRssHandler.
	/// </summary>
	public class ResLinkRssHandler : BaseSyndicationHandler
	{
		public ResLinkRssHandler()
		{

		}
	
		private int SectionID = -1;


		protected override void ProcessFeed()
		{
			if(Context.Request.QueryString["SectionID"] != null)
			{
				double sid = 0;
				if(Double.TryParse( Context.Request.QueryString["SectionID"], System.Globalization.NumberStyles.Integer, null, out sid ))
					this.SectionID = (int)sid;
			}
			
			base.ProcessFeed ();
		}

		protected override CachedFeed BuildFeed()
		{	
			BaseRssWriter writer = null;

			ResLinkSection section = GetCurrentSection as ResLinkSection;

			writer = new ResLinkRssWriter(new ArrayList(section.Links) , section ,  this.BaseUrl);

			DateTime dt = DateTime.MinValue;

			foreach(ResLinkLink lnk in section.Links)
			{
				if (lnk.PostDate > dt)
					dt = lnk.PostDate;
			}
            
			return new CachedFeed(dt,null,writer.GetXml());
		}

		protected override string CacheKey
		{
			get
			{
				return string.Format("SectionID:{0}",SectionID);
			}
		}

		protected override CacheItemPriority Priority
		{
			get
			{
				return CacheItemPriority.Normal;
			}
		}

		protected override int CacheTime
		{
			get
			{
				return CSCache.SecondFactorCalculate(60);
			}
		}

		/// <summary>
		/// Appends http://Host:Port to all blog urls
		/// </summary>
		protected override string BaseUrl
		{
			get
			{
				return Globals.HostPath(Context.Request.Url);
			}
		}

		protected override Section GetCurrentSection
		{
			get
			{
				if(CurrentSection == null || SectionID != CurrentSection.SectionID)
				{
					CurrentSection = ResLinkSection.GetSection(SectionID);
					Permissions.AccessCheck(CurrentSection,Permission.View,CSContext.Current.User);
				}
				
				return CurrentSection;
			}
		}
	}
}
